<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.csgrandeur.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.22.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"default"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="线性表 线性表是数据结构的基础，分为顺序存储与链式存储。顺序表通过数组实现，支持快速随机访问，适用于频繁读取的场景；链表通过节点链接实现，适合频繁增删操作。本文详细介绍了顺序表的基本操作、STL中的vector容器、二分查找算法，以及单链表、循环链表和双向链表的实现与应用。通过实例演示了线性表在多项式表示中的使用，帮助掌握线性表的核心概念与操作技巧。">
<meta property="og:type" content="article">
<meta property="og:title" content="05.线性表">
<meta property="og:url" content="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/index.html">
<meta property="og:site_name" content="CSGrandeur&#39;s Thinking">
<meta property="og:description" content="线性表 线性表是数据结构的基础，分为顺序存储与链式存储。顺序表通过数组实现，支持快速随机访问，适用于频繁读取的场景；链表通过节点链接实现，适合频繁增删操作。本文详细介绍了顺序表的基本操作、STL中的vector容器、二分查找算法，以及单链表、循环链表和双向链表的实现与应用。通过实例演示了线性表在多项式表示中的使用，帮助掌握线性表的核心概念与操作技巧。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/bsearch.gif">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/linked_list.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/linked_list_loop.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/linked_list_loop_merge.svg">
<meta property="article:published_time" content="2025-03-06T10:31:25.000Z">
<meta property="article:modified_time" content="2025-03-14T02:02:59.733Z">
<meta property="article:author" content="CSGrandeur">
<meta property="article:tag" content="ACM">
<meta property="article:tag" content="Algorithm">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/bsearch.gif">


<link rel="canonical" href="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/","path":"2025-03-06-05-线性表/","title":"05.线性表"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>05.线性表 | CSGrandeur's Thinking</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?7958adf931092425a489778560129144"></script>







  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">CSGrandeur's Thinking</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Cogito Ergo Sum</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BA%BF%E6%80%A7%E8%A1%A8"><span class="nav-number">1.</span> <span class="nav-text">线性表</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BA%BF%E6%80%A7%E8%A1%A8%E7%9A%84%E9%A1%BA%E5%BA%8F%E5%AD%98%E5%82%A8"><span class="nav-number">1.1.</span> <span class="nav-text">线性表的顺序存储</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A1%BA%E5%BA%8F%E8%A1%A8%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-number">1.1.1.</span> <span class="nav-text">顺序表基本操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#stl%E7%94%A8vector%E4%BD%9C%E4%B8%BA%E9%A1%BA%E5%BA%8F%E8%A1%A8"><span class="nav-number">1.1.2.</span> <span class="nav-text">STL：用vector作为顺序表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE"><span class="nav-number">1.1.3.</span> <span class="nav-text">二分查找</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BA%BF%E6%80%A7%E8%A1%A8%E7%9A%84%E9%93%BE%E5%BC%8F%E5%AD%98%E5%82%A8"><span class="nav-number">1.2.</span> <span class="nav-text">线性表的链式存储</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%95%E9%93%BE%E8%A1%A8"><span class="nav-number">1.2.1.</span> <span class="nav-text">单链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%95%E9%93%BE%E8%A1%A8%E7%9A%84%E9%9D%99%E6%80%81%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.2.2.</span> <span class="nav-text">单链表的静态实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BE%AA%E7%8E%AF%E9%93%BE%E8%A1%A8"><span class="nav-number">1.2.3.</span> <span class="nav-text">循环链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%8C%E5%90%91%E9%93%BE%E8%A1%A8"><span class="nav-number">1.2.4.</span> <span class="nav-text">双向链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%83%E4%B9%A0%E4%B8%80%E5%85%83%E5%A4%9A%E9%A1%B9%E5%BC%8F"><span class="nav-number">1.2.5.</span> <span class="nav-text">小练习：一元多项式</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">CSGrandeur</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">72</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://blog.csgrandeur.cn/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="CSGrandeur">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="05.线性表 | CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          05.线性表
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-03-06 18:31:25" itemprop="dateCreated datePublished" datetime="2025-03-06T18:31:25+08:00">2025-03-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2025-03-14 10:02:59" itemprop="dateModified" datetime="2025-03-14T10:02:59+08:00">2025-03-14</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/" itemprop="url" rel="index"><span itemprop="name">ACM</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/ACMCOURSE/" itemprop="url" rel="index"><span itemprop="name">ACMCOURSE</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="线性表">线性表</h1>
<p>线性表是数据结构的基础，分为顺序存储与链式存储。顺序表通过数组实现，支持快速随机访问，适用于频繁读取的场景；链表通过节点链接实现，适合频繁增删操作。本文详细介绍了顺序表的基本操作、STL中的vector容器、二分查找算法，以及单链表、循环链表和双向链表的实现与应用。通过实例演示了线性表在多项式表示中的使用，帮助掌握线性表的核心概念与操作技巧。</p>
<span id="more"></span>
<h2 id="线性表的顺序存储">线性表的顺序存储</h2>
<h3 id="顺序表基本操作">顺序表基本操作</h3>
<p><strong>逻辑上相邻的数据，存储在物理上相邻的存储单元</strong></p>
<p>不同变量类型可能占据不同大小的内存，比如<code>char</code>占 <span
class="math inline">\(1\)</span> 字节，<code>int</code> 占 <span
class="math inline">\(4\)</span> 字节，下表用 <span
class="math inline">\(b\)</span> 表示数组 <span
class="math inline">\(a\)</span> 的起始地址， <span
class="math inline">\(L\)</span>表示数组单个元素占内存大小。</p>
<table>
<colgroup>
<col style="width: 10%" />
<col style="width: 5%" />
<col style="width: 6%" />
<col style="width: 6%" />
<col style="width: 13%" />
<col style="width: 6%" />
<col style="width: 13%" />
<col style="width: 7%" />
<col style="width: 6%" />
<col style="width: 19%" />
<col style="width: 3%" />
</colgroup>
<thead>
<tr class="header">
<th>存储地址：</th>
<th>b</th>
<th>b+L</th>
<th>...</th>
<th>b+(i-1)L</th>
<th>...</th>
<th>b+(n-1)L</th>
<th>b+nL</th>
<th>...</th>
<th>b+(maxlen-1)L</th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>内存状态：</td>
<td>a[0]</td>
<td>a[1]</td>
<td>...</td>
<td>a[i]</td>
<td>...</td>
<td>a[n]</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td>线性表位序：</td>
<td>0</td>
<td>1</td>
<td>...</td>
<td>i</td>
<td>...</td>
<td>n</td>
<td>空闲地址</td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>基本操作</p>
<ul>
<li>单个增加</li>
<li>单个插入</li>
<li>单个缩短</li>
<li>单个删除</li>
<li>批量增加</li>
<li>批量插入</li>
<li>定长缩短</li>
<li>批量删除</li>
<li>特定值存在性判断</li>
<li>元素非重复式插入</li>
<li>无序表非重复合并</li>
<li>有序表合并</li>
</ul>
<p>单个插入示例</p>
<table>
<thead>
<tr class="header">
<th>下标</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>数值</td>
<td>12</td>
<td>32</td>
<td>50</td>
<td>17</td>
<td>18</td>
<td>30</td>
<td>27</td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td></td>
<td></td>
<td></td>
<td></td>
<td>插入33</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>插入后</td>
<td>12</td>
<td>32</td>
<td>50</td>
<td>33</td>
<td>17</td>
<td>18</td>
<td>30</td>
<td>27</td>
<td></td>
</tr>
</tbody>
</table>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> MAXLEN=<span class="number">100</span>;   <span class="comment">// 要求至多存100个元素</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">110</span>], len;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">Insert</span><span class="params">(<span class="type">int</span> a[], <span class="type">int</span> x, <span class="type">int</span> &amp;len, <span class="type">int</span> ith)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 将x插入坐标ith，超过表限制长度则报错，并维护表长度</span></span><br><span class="line">    <span class="keyword">if</span>(len == MAXLEN) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;          <span class="comment">// 假设-1为报错信号</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = len; i &gt; ith; i --) &#123;</span><br><span class="line">        a[i] = a[i - <span class="number">1</span>];    <span class="comment">// 把ith位置之后的元素向后挪</span></span><br><span class="line">    &#125;</span><br><span class="line">    a[ith] = x;             <span class="comment">// ith位置已空出，放入x</span></span><br><span class="line">    len ++;                 <span class="comment">// 维护元素个数</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    len = <span class="number">0</span>;    <span class="comment">// 最初没有元素</span></span><br><span class="line">    <span class="built_in">Insert</span>(a, <span class="number">3</span>, len, <span class="number">0</span>);</span><br><span class="line">    <span class="built_in">Insert</span>(a, <span class="number">1</span>, len, <span class="number">1</span>);</span><br><span class="line">    <span class="built_in">Insert</span>(a, <span class="number">5</span>, len, <span class="number">2</span>);</span><br><span class="line">    <span class="built_in">Insert</span>(a, <span class="number">8</span>, len, <span class="number">1</span>);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i ++) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, a[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    <span class="comment">// 查看输出是否为 3 8 1 5 </span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>批量删除示例，按左闭右开区间 <span class="math inline">\([s,
e)\)</span>进行批量删除</p>
<table>
<thead>
<tr class="header">
<th>下标</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>数值</td>
<td>12</td>
<td>32</td>
<td>50</td>
<td>17</td>
<td>18</td>
<td>30</td>
<td>27</td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td></td>
<td></td>
<td></td>
<td></td>
<td>s</td>
<td></td>
<td>e</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>插入后</td>
<td>12</td>
<td>32</td>
<td>50</td>
<td>30</td>
<td>27</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">int</span> MAXLEN=<span class="number">100</span>;   <span class="comment">// 要求至多存100个元素</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">110</span>], len;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">DelRange</span><span class="params">(<span class="type">int</span> a[], <span class="type">int</span> x, <span class="type">int</span> &amp;len, <span class="type">int</span> s, <span class="type">int</span> e)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 删除 [s, e) 左闭右开区间内的元素，也就是把 e 之后所有元素平移到 s</span></span><br><span class="line">    <span class="type">int</span> i = s, j = <span class="built_in">min</span>(len, e);</span><br><span class="line">    <span class="keyword">for</span>(; j &lt; len; i ++, j ++) &#123;</span><br><span class="line">        a[i] = j;</span><br><span class="line">    &#125;</span><br><span class="line">    len = i;    <span class="comment">// 平移结束时，i 指向的就是最后一个元素的下一个位置</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="stl用vector作为顺序表">STL：用vector作为顺序表</h3>
<p>C++的STL提供了vector容器，可以更灵活动态地创建顺序表并使用内置的功能。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="function">std::vector&lt;<span class="type">int</span>&gt; <span class="title">a</span><span class="params">(<span class="number">10</span>)</span></span>;         <span class="comment">// 初始化一个长为10的vector</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%lu\n&quot;</span>, a.<span class="built_in">size</span>());</span><br><span class="line">    <span class="comment">// 上句输出 10</span></span><br><span class="line">    <span class="function">std::vector&lt;<span class="type">int</span>&gt; <span class="title">b</span><span class="params">(<span class="number">5</span>, <span class="number">3</span>)</span></span>;       <span class="comment">// 初始化一个长为5，所有值为3的vector</span></span><br><span class="line">    <span class="comment">// 用传统循环遍历</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; b.<span class="built_in">size</span>(); i ++) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, b[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 上句输出 3 3 3 3 3</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    b.<span class="built_in">insert</span>(b.<span class="built_in">begin</span>() + <span class="number">3</span>, <span class="number">999</span>);   <span class="comment">// 在下标 3 位置插入 999</span></span><br><span class="line">    <span class="comment">// 用迭代器 iterator 遍历</span></span><br><span class="line">    <span class="keyword">for</span>(std::vector&lt;<span class="type">int</span>&gt;::iterator it = b.<span class="built_in">begin</span>(); it != b.<span class="built_in">end</span>(); it ++) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, *it);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 上句输出 3 3 3 999 3 3</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    b.<span class="built_in">push_back</span>(<span class="number">666</span>);               <span class="comment">// 在末尾插入 666</span></span><br><span class="line">    <span class="comment">// 用C++11开始支持的“auto”遍历</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">auto</span> x : b) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, x);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 上句输出 3 3 3 999 3 3 666</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    b.<span class="built_in">resize</span>(<span class="number">200</span>);                  <span class="comment">// 大小改为200，扩展出的数值未定义</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%lu\n&quot;</span>, b.<span class="built_in">size</span>());</span><br><span class="line">    <span class="comment">// 上句输出 200</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>vector更多丰富功能可参考<a
target="_blank" rel="noopener" href="https://zh.cppreference.com/w/cpp/container/vector">CPP官方文档</a></p>
<h3 id="二分查找">二分查找</h3>
<p>在一个已经从小到大有序的数组中查找特定数值的位置，可以有比一遍循环挨个查看更快的方法.</p>
<p>猜数小游戏：想一个 <span
class="math inline">\(1000\)</span>以内的整数，让朋友猜，只提示高了还是低了.
显而易见的策略是先猜 <span
class="math inline">\(500\)</span>，如果低了那么下一次就猜 <span
class="math inline">\(750\)</span>，如果高了就猜 <span
class="math inline">\(250\)</span>，很快（大约 <span
class="math inline">\(10\)</span> 次）就能得到答案.</p>
<p>在有序数组上的二分查找就是这个道理，可以快速判断特定数值是否在数组中存在，以及在哪个位置.</p>
<img src="/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/bsearch.gif" class="" title="二分查找">
<p>上图对应这样一份二分查找代码：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">100</span>], length; <span class="comment">// 有序</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">BiSearch</span><span class="params">(<span class="type">int</span> num, <span class="type">int</span> a[], <span class="type">int</span> alen)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// left right 标识左闭右开区间</span></span><br><span class="line">    <span class="comment">// 即 left 指向首元素，right指向末元素的下一个位置</span></span><br><span class="line">    <span class="type">int</span> left = <span class="number">0</span>, right = alen;</span><br><span class="line">    <span class="keyword">while</span>(left &lt; right - <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="comment">// left与right的中间位置，这里右移运算与除以2等价</span></span><br><span class="line">        <span class="type">int</span> mid = left + right &gt;&gt; <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span>(a[mid] &lt;= num) left = mid;</span><br><span class="line">        <span class="keyword">else</span> right = mid; <span class="comment">// right 保持考察区间右侧开区间，这里已排除mid</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// left 的位置是搜索结果</span></span><br><span class="line">    <span class="keyword">return</span> a[left] == num ? left : <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当有序数组中有重复元素时，需求可能会分为 Lower Bound 和 Upper
Bound，他们的定义是：</p>
<ul>
<li><strong>Lower Bound</strong>：对于给定的元素 <span
class="math inline">\(x\)</span>，不小于 <span
class="math inline">\(x\)</span> 的第一个元素的位置</li>
<li><strong>Upper Bound</strong>：对于给定的元素 <span
class="math inline">\(x\)</span>，大于 <span
class="math inline">\(x\)</span>的第一个元素的位置</li>
</ul>
<p>参考代码：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">LowerBound</span><span class="params">(<span class="type">int</span> a[], <span class="type">int</span> n, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 在元素个数为 n 的有序数组 a[] 中查找 x 的 Lower Bound</span></span><br><span class="line">    <span class="type">int</span> left = <span class="number">0</span>, right = n, mid;</span><br><span class="line">    <span class="keyword">while</span>(left &lt; right) &#123;</span><br><span class="line">        mid = (left + right) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span>(a[mid] &gt;= x) right = mid;</span><br><span class="line">        <span class="keyword">else</span> left = mid + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> left == n ? <span class="number">-1</span> : left;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">UpperBound</span><span class="params">(<span class="type">int</span> a[], <span class="type">int</span> n, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 在元素个数为 n 的有序数组 a[] 中查找 x 的 Upper Bound</span></span><br><span class="line">    <span class="type">int</span> left = <span class="number">0</span>, right = n, mid;</span><br><span class="line">    <span class="keyword">while</span>(left &lt; right) &#123;</span><br><span class="line">        mid = (left + right) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">if</span>(a[mid] &gt; x) right = mid;</span><br><span class="line">        <span class="keyword">else</span> left = mid + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> left == n ? <span class="number">-1</span> : left;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>会发现 LowerBound 与 UpperBound
有微妙的区别，为了加深理解，可以自己设计一个有重复元素的有序数组，模拟一下过程，或者用调试的方式单步执行。</p>
<p>二分查找的写法并不唯一，比如前面动图的代码和后面的 Lower Bound
写法就有区别，新手很容易在处理边界时困惑或出错，比如
<code>if(a[mid] &gt; x)</code>还是<code>if(a[mid] &gt;= x)</code>？为什么
<code>right = mid</code> 而 <code>left = mid + 1</code>？循环条件是
<code>left &lt; right</code> 还是 <code>left &lt;= right</code> ？</p>
<p>在手动模拟过程理解一份正确的二分查找代码后，<strong>把它背下来</strong>，可以减少未来可能的出错。</p>
<p>接下来也给出使用<code>vector</code>和<code>stl</code>内置的实现方法，一些<code>stl</code>使用细节不明白的地方可以先跳过或查询资料，当以后熟练了再回来看。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">LowerBound</span><span class="params">(std::vector&lt;<span class="type">int</span>&gt;&amp; a, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">auto</span> it = std::<span class="built_in">lower_bound</span>(a.<span class="built_in">begin</span>(), a.<span class="built_in">end</span>(), x);</span><br><span class="line">    <span class="keyword">return</span> it == a.<span class="built_in">end</span>() ? <span class="number">-1</span> : it - a.<span class="built_in">begin</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">UpperBound</span><span class="params">(std::vector&lt;<span class="type">int</span>&gt;&amp; a, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">auto</span> it = std::<span class="built_in">upper_bound</span>(a.<span class="built_in">begin</span>(), a.<span class="built_in">end</span>(), x);</span><br><span class="line">    <span class="keyword">return</span> it == a.<span class="built_in">end</span>() ? <span class="number">-1</span> : it - a.<span class="built_in">begin</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="线性表的链式存储">线性表的链式存储</h2>
<h3 id="单链表">单链表</h3>
<p>逻辑上相邻的数据元素在物理上不一定相邻</p>
<table>
<thead>
<tr class="header">
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th>⬇头指针</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>存储地址：</td>
<td>1</td>
<td>7</td>
<td>13</td>
<td>19</td>
<td>25</td>
<td>31</td>
<td>37</td>
<td>43</td>
</tr>
<tr class="even">
<td>数据域：</td>
<td>LI</td>
<td>QIAN</td>
<td>SUN</td>
<td>WANG</td>
<td>WU</td>
<td>ZHAO</td>
<td>ZHENG</td>
<td>ZHOU</td>
</tr>
<tr class="odd">
<td>指针域：</td>
<td>43</td>
<td>13</td>
<td>1</td>
<td>NULL</td>
<td>37</td>
<td>7</td>
<td>19</td>
<td>25</td>
</tr>
</tbody>
</table>
<img src="/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/linked_list.svg" class="" title="链表示例">
<p>基本定义</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="type">int</span> val;    <span class="comment">// 值</span></span><br><span class="line">    Node *nex;  <span class="comment">// 指针</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">AddHead</span><span class="params">(Node *hp, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 将数 x 插入头节点为 head 的链表开头——头插法</span></span><br><span class="line">    Node *p = <span class="keyword">new</span> Node;</span><br><span class="line">    p-&gt;val = x;</span><br><span class="line">    p-&gt;nex = hp-&gt;nex;</span><br><span class="line">    hp-&gt;nex = p;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">AddTail</span><span class="params">(Node *hp, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 将数 x 插入头节点为 head 的链表结尾——尾插法</span></span><br><span class="line">    Node *p = <span class="keyword">new</span> Node;</span><br><span class="line">    Node *r;</span><br><span class="line">    <span class="keyword">for</span>(r = hp; r-&gt;nex != <span class="literal">nullptr</span>; r = r-&gt;nex);  <span class="comment">// 找到链表末尾</span></span><br><span class="line">    p-&gt;val = x;</span><br><span class="line">    p-&gt;nex = r-&gt;nex;</span><br><span class="line">    r-&gt;nex = p;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Node *head = <span class="keyword">new</span> Node;  <span class="comment">// 头节点</span></span><br><span class="line">    head-&gt;nex = <span class="literal">nullptr</span>;</span><br><span class="line">    <span class="built_in">AddHead</span>(head, <span class="number">1</span>);</span><br><span class="line">    <span class="built_in">AddHead</span>(head, <span class="number">3</span>);</span><br><span class="line">    <span class="built_in">AddTail</span>(head, <span class="number">4</span>);</span><br><span class="line">    <span class="keyword">for</span>(Node *p = head-&gt;nex; p != <span class="literal">nullptr</span>; p = p-&gt;nex) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, p-&gt;val);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>基本操作</p>
<ul>
<li>单个增加</li>
<li>单个插入</li>
<li>单个缩短</li>
<li>单个删除</li>
<li>批量增加</li>
<li>批量插入</li>
<li>定长缩短</li>
<li>批量删除</li>
<li>特定值存在性判断</li>
<li>元素非重复式插入</li>
<li>无序表非重复合并</li>
<li>有序表合并</li>
</ul>
<p>思考：为什么要有不存数值的“头节点”？</p>
<ul>
<li>尝试分别用有无头节点的方式执行基本操作</li>
<li>考虑有无头节点情况下空表分别如何体现</li>
</ul>
<p>思考：什么时候用顺序表，什么时候用链表？</p>
<ul>
<li>顺序表方便随机存取：需要频繁读取第x个元素的任务</li>
<li>链表方便频繁增删：需要频繁增删部分元素的任务</li>
</ul>
<h3 id="单链表的静态实现">单链表的静态实现</h3>
<p>算法竞赛用固定可控的内存管理更不易出错，链表指针无非表达数据存储的位置，数组下标一样可以实现。</p>
<p><strong>推荐在比赛中用静态数组与下标的方式管理链式结构，尽量避免使用指针</strong>。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="type">int</span> val;</span><br><span class="line">    <span class="type">int</span> nex;    <span class="comment">// 指针改为数组下标</span></span><br><span class="line">&#125;;</span><br><span class="line">Node lst[<span class="number">999</span>];  <span class="comment">// 提前开好足够题目使用的内存作为待用链表节点</span></span><br><span class="line"><span class="type">int</span> tp;         <span class="comment">// 一个模拟内存控制的下标变量</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">NewNode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 模拟新建一个节点，返回lst中的下标</span></span><br><span class="line">    lst[tp].nex = <span class="number">-1</span>;   <span class="comment">// 用 -1 下标表达“空指针”的含义</span></span><br><span class="line">    tp ++;              <span class="comment">// 让 tp 指向下一个待使用的Node</span></span><br><span class="line">    <span class="keyword">return</span> tp - <span class="number">1</span>;      <span class="comment">// tp刚刚++了，tp-1才是刚分配好的Node</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">AddHead</span><span class="params">(<span class="type">int</span> hp, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 将数 x 插入头节点下标为 hp 的链表开头——头插法</span></span><br><span class="line">    <span class="type">int</span> p = <span class="built_in">NewNode</span>();</span><br><span class="line">    lst[p].val = x;</span><br><span class="line">    lst[p].nex = lst[hp].nex;</span><br><span class="line">    lst[hp].nex = p;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">AddTail</span><span class="params">(<span class="type">int</span> hp, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 将数 x 插入头节点为 head 的链表结尾——尾插法</span></span><br><span class="line">    <span class="type">int</span> p = <span class="built_in">NewNode</span>(), r;</span><br><span class="line">    <span class="keyword">for</span>(r = hp; lst[r].nex != <span class="number">-1</span>; r = lst[r].nex);  <span class="comment">// 找到链表末尾</span></span><br><span class="line">    lst[p].val = x;</span><br><span class="line">    lst[p].nex = lst[r].nex;</span><br><span class="line">    lst[r].nex = p;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    tp = <span class="number">0</span>;     <span class="comment">// 初始化节点分配下标为0</span></span><br><span class="line">    <span class="type">int</span> head = <span class="built_in">NewNode</span>();</span><br><span class="line">    <span class="built_in">AddHead</span>(head, <span class="number">1</span>);</span><br><span class="line">    <span class="built_in">AddHead</span>(head, <span class="number">3</span>);</span><br><span class="line">    <span class="built_in">AddTail</span>(head, <span class="number">4</span>);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> p = lst[head].nex; p != <span class="number">-1</span>; p = lst[p].nex) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, lst[p].val);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="循环链表">循环链表</h3>
<p>给出一个单链表中间某个节点的指针，无法找到它之前的节点。</p>
<p>如果把链表首尾相连，让最后一个节点指向头节点，就可以从任何位置出发找到所有节点。</p>
<img src="/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/linked_list_loop.svg" class="" title="循环链表">
<p>小练习：仅知道尾指针<code>rearA</code>与<code>rearB</code>的情况下合并两个循环链表为一个循环链表</p>
<img src="/2025-03-06-05-%E7%BA%BF%E6%80%A7%E8%A1%A8/linked_list_loop_merge.svg" class="" title="循环链表">
<p>尝试补全代码</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="type">int</span> val;</span><br><span class="line">    <span class="type">int</span> nex;</span><br><span class="line">&#125;;</span><br><span class="line">Node lst[<span class="number">9999</span>];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Merge</span><span class="params">(<span class="type">int</span> rearA, <span class="type">int</span> rearB)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// lst[rearA]和lst[rearB]分别是两个循环链表最后节点</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="双向链表">双向链表</h3>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">DNode</span> &#123;</span><br><span class="line">    <span class="type">int</span> val;</span><br><span class="line">    <span class="type">int</span> prior;  <span class="comment">// 指向上一个节点</span></span><br><span class="line">    <span class="type">int</span> nex;    <span class="comment">// 指向下一个节点</span></span><br><span class="line">&#125;;</span><br><span class="line">DNode lst[<span class="number">9999</span>];</span><br><span class="line"><span class="type">int</span> tp = <span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">NewNode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>尝试在 <code>p</code> 节点之前插入元素 <code>x</code></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">DLInsert</span><span class="params">(<span class="type">int</span> p, <span class="type">int</span> x)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>尝试在删除<code>p</code>节点</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">DLDelete</span><span class="params">(<span class="type">int</span> p)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>参考答案如下</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">DLInsert</span>(<span class="type">int</span> p, <span class="type">int</span> x) &#123;</span><br><span class="line">    <span class="type">int</span> np = <span class="built_in">NewNode</span>();</span><br><span class="line">    lst[np].val = x;</span><br><span class="line">    <span class="comment">// 设定新节点 np 的前后下标</span></span><br><span class="line">    lst[np].nex = p;</span><br><span class="line">    lst[np].prior = lst[p].prior;</span><br><span class="line">    <span class="comment">// 更新 p 之前节点的后下标</span></span><br><span class="line">    lst[lst[p].prior].nex = np;</span><br><span class="line">    <span class="comment">// 更新 p 节点的前下标</span></span><br><span class="line">    lst[p].prior = np;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">DLDelete</span>(<span class="type">int</span> p) &#123;</span><br><span class="line">    <span class="comment">// 更新 p 之前节点的后下标</span></span><br><span class="line">    lst[lst[p].prior].nex = lst[p].nex;</span><br><span class="line">    lst[lst[p].nex].prior = lst[p].prior;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="小练习一元多项式">小练习：一元多项式</h3>
<p>稠密一元多项式：各项指数相差不大</p>
<p><span
class="math inline">\(P(x)=10+5x-4x^{2}+3x^{3}+2x^{4}\)</span></p>
<p>用顺序表表达</p>
<table>
<thead>
<tr class="header">
<th>指数 x</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>系数 p[x]</td>
<td>10</td>
<td>5</td>
<td>-4</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> p[<span class="number">999</span>];</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> n;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n);                <span class="comment">// n项</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i ++) &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;p[i])          <span class="comment">// 读取 0~n-1各项系数</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>稀疏一元多项式：指数差别很大，比如 <span
class="math inline">\(2x^{1352412369}+x^{1000000000}\)</span></p>
<p>用链表表达</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Node</span> &#123;</span><br><span class="line">    <span class="type">int</span> c;      <span class="comment">// 系数</span></span><br><span class="line">    <span class="type">int</span> e;      <span class="comment">// 指数</span></span><br><span class="line">    <span class="type">int</span> nex;    <span class="comment">// 链表下一个节点下标</span></span><br><span class="line">&#125;;</span><br><span class="line">Node lst[<span class="number">9999</span>];</span><br><span class="line"><span class="type">int</span> tp;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">NewNode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">AddTail</span><span class="params">(<span class="type">int</span> hp, <span class="type">int</span> c, <span class="type">int</span> e)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// TODO</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> n, e;</span><br><span class="line">    tp = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> head = <span class="built_in">NewNode</span>();</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;n);                <span class="comment">// n项，但指数不保证连续</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i ++) &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>, &amp;c, &amp;e);      <span class="comment">// 输入保证 e 从小到大递增</span></span><br><span class="line">        <span class="type">int</span> p = <span class="built_in">NewNode</span>();</span><br><span class="line">        lst[p].c = c;</span><br><span class="line">        lst[p].e = e;</span><br><span class="line">        <span class="built_in">AddTail</span>(head, c, e);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>思考：两个这样的多项式相加如何计算和保存结果<br />
提示：因为 e
递增，可以两个游标分别对应两个链表异步对齐前进，新结果插入到新链表中</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ACM/" rel="tag"># ACM</a>
              <a href="/tags/Algorithm/" rel="tag"># Algorithm</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2025-03-06-04-%E5%87%BD%E6%95%B0%E4%B8%8E%E7%BB%93%E6%9E%84%E4%BD%93/" rel="prev" title="04.函数与结构体">
                  <i class="fa fa-angle-left"></i> 04.函数与结构体
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2025-03-06-06-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%9F%BA%E7%A1%80/" rel="next" title="06.字符串基础">
                  06.字符串基础 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">CSGrandeur</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://fastly.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://fastly.jsdelivr.net/npm/@next-theme/pjax@0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://fastly.jsdelivr.net/npm/hexo-generator-searchdb@1.4.1/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>







  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://fastly.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"CSGrandeur/csgrandeur.github.io","issue_term":"pathname","theme":"github-light"}</script>
<script src="/js/third-party/comments/utterances.js"></script>

</body>
</html>
